package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Publisher;

public class PublisherDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Publisher publisher) {
		open();
		this.sql = "INSERT INTO Publishers (name_publisher, id_contact_publisher) VALUES (?, ?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, publisher.getNamePublisher());
			this.statement.setInt(2, publisher.getIdContactPublisher());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Publisher publisher) {
		open();
		this.sql = "UPDATE Publishers SET name_publisher = ?, id_contact_publisher = ? WHERE id_publisher = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, publisher.getNamePublisher());
			this.statement.setInt(2, publisher.getIdContactPublisher());
			this.statement.setInt(3, publisher.getIdPublisher());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Publisher publisher) {
		open();
		this.sql = "DELETE FROM Publishers WHERE id_publisher = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, publisher.getIdPublisher());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Publisher search(Integer id_publisher) {
		open();
		this.sql = "SELECT * FROM Publishers WHERE id_publisher = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_publisher);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Publisher(result.getInt("id_publisher"),
						result.getString("name_publisher"),
						result.getInt("id_contact_publisher"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Publisher> list() {
		open();
		this.sql = "SELECT * FROM Publishers";
		ArrayList<Publisher> publishers = new ArrayList<Publisher>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				publishers.add(new Publisher(result.getInt("id_publisher"),
						result.getString("name_publisher"), result
								.getInt("id_contact_publisher")));
			}
			return publishers;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
